package com.examples.cmw.android_study_demos;

import android.media.Image;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleComponetActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private TextView tv_simple_label;
    private Button btnSubmit;
    private EditText ed_simple_number;
    private ImageView iv_simple_play;

    // 5. CheckBox Demo
    private CheckBox cb_simple_basket;
    private CheckBox cb_simple_foot;
    private CheckBox cb_simple_pingpang;

    // 6. RadioGroup/RadioButton Demo
    private RadioGroup rg_simple_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_componet);

        // 1. TextView
        tv_simple_label = findViewById(R.id.tv_simple_label);
        tv_simple_label.setText("Android UI 简单控件示例");

        // 2. EditText
        ed_simple_number = findViewById(R.id.ed_simple_number);

        // 3. Button
        btnSubmit = findViewById(R.id.btn_simple_submit);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String val = ed_simple_number.getText().toString();
                Log.i(TAG, "val = "+val);
                Toast.makeText(SimpleComponetActivity.this, val, Toast.LENGTH_LONG).show();
            }
        };
        btnSubmit.setOnClickListener(onClickListener);

        // 4. ImageView
        iv_simple_play = findViewById(R.id.iv_simple_play);
        iv_simple_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_simple_play.setBackgroundResource(android.R.drawable.alert_light_frame);
                iv_simple_play.setImageResource(android.R.drawable.ic_media_pause);
            }
        });

        // 5. CheckBox
        cb_simple_basket = findViewById(R.id.cb_simple_basket);
        cb_simple_foot = findViewById(R.id.cb_simple_foot);
        cb_simple_pingpang = findViewById(R.id.cb_simple_pingpang);
        cb_simple_foot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String tip = isChecked ? "选中了足球" : "未选中了足球";
                Toast.makeText(SimpleComponetActivity.this, tip, Toast.LENGTH_LONG).show();
            }
        });

        // 6. RadioGroup/RadioButton
        rg_simple_sex = findViewById(R.id.rg_simple_sex);
        rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radio = findViewById(checkedId);
                String val = radio.getText().toString();
                Toast.makeText(SimpleComponetActivity.this, val, Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     *  5. 选择爱好事件，直接在 XML 文件指定的事件方法
     * @param view
     */
    public void confirm(View view) {
        StringBuffer sb = new StringBuffer();
        if (cb_simple_basket.isChecked()) {
            sb.append(cb_simple_basket.getText().toString()).append(" ");
        }

        if (cb_simple_foot.isChecked()) {
            sb.append(cb_simple_foot.getText().toString()).append(" ");
        }

        if (cb_simple_pingpang.isChecked()) {
            sb.append(cb_simple_pingpang.getText().toString());
        }
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
    }
}
